package com.atiffarrukh.get.inspired;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.plus.PlusClient;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.support.v4.app.ShareCompat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.widget.Toast;

public class GooglePlusLogInandPost extends Activity implements ConnectionCallbacks, OnConnectionFailedListener,
																OnClickListener{
	private static final String TAG = "GoolgePlusLogInandPostActivity";
	private String notConnected = "You are not connected to any Network"; 
	
    private static final int REQUEST_CODE_RESOLVE_ERR = 9000;

    private ProgressDialog mConnectionProgressDialog;
    private PlusClient mPlusClient;
    private ConnectionResult mConnectionResult;
    private static final String site = "https://play.google.com/store/apps/details?id=com.atiffarrukh.get.inspired";
    String postQuote;
        
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goolge_plus_log_in_and_post);
        //Log.d(TAG, "onCreate()");

        
        if(isConntected()){
        	
        	mPlusClient = new PlusClient(this, this, this, Scopes.PLUS_PROFILE);
            // Progress bar to be displayed if the connection failure is not resolved.
            mConnectionProgressDialog = new ProgressDialog(this);
            mConnectionProgressDialog.setMessage("Signing in...");
            findViewById(R.id.sign_in_button).setOnClickListener(this);
            Bundle b = getIntent().getExtras();
      		postQuote = b.getString("postValue") + "\n" + site;
      		mConnectionProgressDialog.show();
        }else{
        	Log.d(TAG, notConnected);
        	finish();
        	//Toast.makeText(getBaseContext(), notConnected, Toast.LENGTH_LONG).show();
        	/*Intent i = new Intent(this, MainActivity.class);
        	startActivity(i);*/
        }
    }
	private boolean isConntected() {
		// TODO Auto-generated method stub
		boolean isConntected = true;
		NetworkInfo activeNetwork;
        ConnectivityManager cm;
        Log.d(TAG, "isConnected()");
        try{
        cm = (ConnectivityManager) this.getSystemService(CONNECTIVITY_SERVICE);
        activeNetwork = cm.getActiveNetworkInfo();
        isConntected = activeNetwork.isConnected();
        }catch(RuntimeException e){
        	isConntected = false;
        	Toast.makeText(this,notConnected, Toast.LENGTH_LONG).show();
        	return isConntected;
        }
        
        Log.d(TAG, "Value of isConnected == " + isConntected);
        return isConntected;
        
	}
	@Override
	public void onConnectionFailed(ConnectionResult result) {
		// TODO Auto-generated method stub
		 if (mConnectionProgressDialog.isShowing()) {
		        // The user clicked the sign-in button already.
		        // Dismiss the progress dialog and start to resolve connection errors.
		        mConnectionProgressDialog.dismiss();

		        if (result.hasResolution()) {
		            try {
		                result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
		            } catch (SendIntentException e) {
		                mPlusClient.connect();
		            }
		        }
		    }

		    // Save the intent so that we can start an activity when the user clicks
		    // the sign-in button.
		    mConnectionResult = result;
	}
	@Override
	public void onConnected() {
		// TODO Auto-generated method stub
		mConnectionProgressDialog.dismiss();
		AlphaAnimation an = new AlphaAnimation(1.0f, 0.0f);
		an.setDuration(100);
		findViewById(R.id.sign_in_button).setVisibility( 4);
		String accountName = mPlusClient.getAccountName();
		Toast.makeText(this, accountName + " is connected.", Toast.LENGTH_LONG).show();
		// Launch the Google+ share dialog with attribution to your app.
	      Intent shareIntent = ShareCompat.IntentBuilder.from(this)
	          .setType("text/plain")
	          .setText(postQuote)
	          .getIntent()
	          .setPackage("com.google.android.apps.plus");

	      startActivity(shareIntent);
	      finish();
	}
	@Override
	public void onDisconnected() {
		// TODO Auto-generated method stub
		Log.d(TAG, "disconnected");
	}
	
	 @Override
	    protected void onStart() {
	        super.onStart();
	        mPlusClient.connect();
	    }

	    @Override
	    protected void onStop() {
	        super.onStop();
	        mPlusClient.disconnect();
	    }
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
		    if (v.getId() == R.id.sign_in_button && !mPlusClient.isConnected()) {
		        if (mConnectionResult == null) {
		            mConnectionProgressDialog.show();
		        } else {
		            try {
		                mConnectionResult.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
		            } catch (SendIntentException e) {
		                // Try connecting again.
		                mConnectionResult = null;
		                mPlusClient.connect();
		            }
		        }
		    }
		}
	    
}
